package com.hgw.easyrocketmq.delay.consumer;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * Description: 延迟消息消费者
 *
 * @author LinHuiBa-YanAn
 * @date 2023/1/15 15:11
 */
public class ScheduledMessageConsumer {

	public static void main(String[] args) throws MQClientException {
		DefaultMQPushConsumer mqPushConsumer = new DefaultMQPushConsumer("group1");
		mqPushConsumer.setNamesrvAddr("124.222.223.222:9876;47.96.232.192:9876");
		// 订阅主题Topic和标签Tag
		mqPushConsumer.subscribe("delayTopic", "*");
		mqPushConsumer.registerMessageListener(new MessageListenerConcurrently() {
			@Override
			public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
				for (MessageExt message : msgs) {
					// Print approximate delay time period
					System.out.println("Receive message[msgId=" + new String(message.getMsgId()) + " body " + message.getBody() + "] " + (System.currentTimeMillis() - message.getStoreTimestamp()) + "ms later");
				}
				return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
			}
		});
		mqPushConsumer.start();
	}

}
